package com.sunday.gateway.core.filter.log;

import lombok.Data;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.util.unit.DataSize;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * 访问日志过滤器工厂
 *
 * @author sunday
 * @since 2024/3/13
 */
@Component
public class AccessLogGatewayFilterFactory
        extends AbstractGatewayFilterFactory<AccessLogGatewayFilterFactory.Config> {

    public AccessLogGatewayFilterFactory() {
        super(Config.class);
    }

    @Override
    public GatewayFilter apply(Config config) {
        AccessLogGatewayFilter gatewayFilter = new AccessLogGatewayFilter(config);
        gatewayFilter.setFactory(this);
        return gatewayFilter;
    }


    @Data
    public static class Config {

        /**
         * UTF-8编码（一个汉字通常占用3个字节），那么5000个汉字
         * 5000 * 3 = 15000
         * 所以默认值保留 5000个汉字左右得信息量
         */
        // 最大请求体大小
        private DataSize maxRequestSize = DataSize.ofBytes(15000L);
        // 最大响应体大小
        private DataSize maxResponseSize = DataSize.ofBytes(15000L);
        // 支持的请求内容类型
        private List<MediaType> supportedRequestMediaTypes = List.of(MediaType.APPLICATION_FORM_URLENCODED, MediaType.APPLICATION_JSON);
        // 支持的响应内容类型
        private List<MediaType> supportedResponseMediaTypes = List.of(MediaType.APPLICATION_JSON);
        // 请求体类型
        private Class<?> bodyClass = String.class;
        // 字符集
        private Charset charset = StandardCharsets.UTF_8;

    }

}
